package com.applcn.shop.repo.repository;

import com.applcn.shop.constant.ShopBindStatusEnum;
import com.applcn.shop.repo.entity.ShopBindEntity;
import com.applcn.shop.repo.mapper.ShopBindMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * com.applcn.shop.repo.repository
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-03-19 00:51
 */
@Repository
public class ShopBindRepository extends ServiceImpl<ShopBindMapper, ShopBindEntity> {

    /**
     * 分页查询商户绑定店铺列表
     *
     * @param pageNum
     * @param pageSize
     * @param merchantId
     * @return
     */
    public IPage<ShopBindEntity> selectShopListByMerchantId(Integer pageNum, Integer pageSize, Long merchantId) {
        Page<ShopBindEntity> page = new Page<>(pageNum, pageSize);
        IPage<ShopBindEntity> shopBindEntityIPage = baseMapper.selectPage(
                page,
                new QueryWrapper<ShopBindEntity>()
                        .lambda()
                        .eq(ShopBindEntity::getMerchantId, merchantId)
                        .eq(ShopBindEntity::getShopBindStatus, ShopBindStatusEnum.ON)
        );

        return shopBindEntityIPage;
    }

    /**
     * 通过店铺id查询绑定管理员
     *
     * @param shopId
     * @return
     */
    public List<ShopBindEntity> listByShopId(Long shopId) {
        return baseMapper.selectList(
                new QueryWrapper<ShopBindEntity>()
                        .lambda()
                        .eq(ShopBindEntity::getShopId, shopId)
                        .eq(ShopBindEntity::getShopBindStatus, ShopBindStatusEnum.ON)
        );
    }

    /**
     * 查询店铺商户绑定关系列表
     *
     * @param shopId
     * @param merchantIds
     * @return
     */
    public List<ShopBindEntity> listByShopIdAndMerchantIds(Long shopId, List<Long> merchantIds) {
        return baseMapper.selectList(
                new QueryWrapper<ShopBindEntity>()
                .lambda()
                .eq(ShopBindEntity::getShopId, shopId)
                .in(ShopBindEntity::getMerchantId, merchantIds)
        );
    }
}
